'''
1、这段时间内餐厅的销售流水总额（5分）
2、这段时间内销售个数最多的商品 （5分）
3、这段时间内销售金额最多的商品（5分）
4、每种商品的销售个数，并做图（10分）
5、每种商品的销售金额，并做图（10分）
6、餐厅的产品的种类及对应的商品配料的价格，做出表格（10分）
7、每种商品销售金额在流水总额中所占的比例，做圆饼图（10分）
8、每个定单金额，并做bar图（10分）
9、产品“Chicken Bowl”,选择最多的配料（5分）
10、产品“Chicken Bowl”每种配料选择的次数，及所占比例，并做圆饼图（10分）
'''

import os
import numpy as np
import pandas as pd
from pyecharts import Bar
from pyecharts import Pie

# Create your ana here.


class ChipotleNan:

    def __init__(self):

        self.csv_name =r'E:\数据分析\datalist\np01\chipotle.csv'

        self.df = pd.read_csv(self.csv_name, sep = '\t')


    def bar(self, axis0, axis1,  name = 'test', table_name = 'test1'):

        bar = Bar(name)

        bar.add("商家A", axis0, axis1, is_label_show=True, is_datazoom_show=True)

        return bar

    def series_to_bar(self, series, name = 'test', table_name = 'test1'):

        bar = Bar(name)
        axis0 = series.index
        axis1 = np.round(series.values.ravel(), 1)

        bar.add("商家A", axis0, axis1, is_label_show=True, is_datazoom_show=True)

        return bar

    #这段时间内餐厅的销售流水总额
    def this_time_sold_brand_total_yuan(self):
        x = self.df.quantity * (self.df.item_price.str.replace('$', '').astype('float64'))
        df1 = self.df.copy()
        df1['price'] = x

        return  df1.price.sum()

    # 这段时间内销售个数最多的商品
    def this_time_sold_brand_very_kind(self):
        # 1.chipo.sort_values(by="quantity", ascending=False).head(1)
        # 2.shu=self.df.groupby('item_name').sum().sort_values(by='quantity')
        chipo= self.df.groupby('item_name')
        c = chipo.sum()
        c = c.sort_values(['quantity'], ascending=False)
        a=self.df.columns
        # print(a)
        # print(c.head(1).values)

        return c.head(1)

    # 这段时间内销售金额最多的商品
    def this_time_sold_money_very_duo(self):
        ipr =self.df.quantity * (self.df.item_price.str.replace('$', '').astype('float64'))
        df1 = self.df.copy()
        df1['ipr'] = ipr
        # a=df1.sort_values(by='ipr',ascending=False).head(1).columns
        # print(df1.sort_values(by='ipr',ascending=False).head(1).values[0][0])
        return df1.sort_values(by='ipr',ascending=False).head(1)


    #-------------------------------------------------------------------------
    # 每种商品的销售个数，并做图
    def how_many_are_sold_per_item(self):

        df1 = self.df.groupby('item_price').sum()

        df1.drop('order_id' ,axis = 1, inplace = True)

        return df1
    def bar_how_many_are_sold_per_item(self):

        #chipo = ana.ChipotleNan()

        info = self.how_many_are_sold_per_item()

        #bar = self.bar(info.index, info.quantity)

        bar = self.series_to_bar(info,'每种商品的销售个数')

        return bar


    # ---------------------------------------------------------------
    # 每种商品的销售金额，并做图
    def how_much_are_sold_per_item(self):

        ipr = self.df.quantity*(self.df.item_price.str.replace('$','').astype('float64'))
        df1 = self.df.copy()
        df1['ipr'] = ipr
        df1_group = df1.groupby('item_name').sum()

        df1_group = df1_group.drop('order_id', axis = 1)
        df1_group = df1_group.drop('quantity', axis = 1)

        return df1_group

    def bar_how_much_are_sold_per_item(self):

        info = self.how_much_are_sold_per_item()

        bar = self.series_to_bar(info, '每种商品销售金额')

        return bar
    #---------------------------------------------------------------
    # 餐厅的产品的种类及对应的商品配料的价格，做出表格
    def product_kinds_and_brand_material_price_to_exls(self):

        info=self.df
        infoo=info.drop("order_id",axis=1)
        # info1=infoo.drop('quantity',axis=1)
        # print(infoo)
        # 名字唯一的商品名
        kinds=infoo.item_name.unique()
        # print(kinds)
        uk=infoo.groupby(['item_name','choice_description','item_price']).sum()
        # print(uk)
        uk.drop('quantity',axis=1,inplace=True)
        # print(uk.index[1])
        # num=len((uk.drop('quantity',axis=1,inplace=True)).index)
        return uk
        # for i in range(0,1915):
        #     aa=uk.index[i]
        #     print(aa)
        # print(uk)
        # info2 = self.how_much_are_sold_per_item()
        # print(info2)
        # print(info1.item_price.sum())
        # print(info1.sort_values(by='item_name').nunique())
        # print(info1.groupby('item_name').nunique())
        # print(info1.item_name,info1.choice_description,info1.item_price)
        # print(len(info1.values))









    #---------------------------------------------------------------
    # 每种商品销售金额在流水总额中所占的比例，做圆饼图
    def The_amount_of_each_item_sold_as_a_percentage_of_the_total_amount(self):
       # 计算格，再除以所有商品的总价是比率
       sold_name=self.df.item_name.unique()
       per_brand_goods = self.how_much_are_sold_per_item()
       print(len(per_brand_goods))
       total_money = self.this_time_sold_brand_total_yuan()
       rate = per_brand_goods / total_money

       shu=rate.ipr
       pie=Pie('饼图')
       pie.add('每种商品销售金额在流水总额中所占的比例',sold_name,shu,is_label_show=True,is_more_utils=True)
       pie.render()


    #---------------------------------------------------------------
    # 每个定单金额，并做bar图
    def amount_per_order_money(self):
        ipr = self.df.quantity * (self.df.item_price.str.replace('$', '').astype('float64'))
        df1 = self.df.copy()
        df1['ipr'] = ipr
        a=df1.ipr
        # print(a)
        sold_name = list(self.df.item_name)
        # print(len(sold_name))
        # print(sold_name)
        bar=Bar('柱状图')
        bar.add('每个订单的金额',sold_name,a,is_datazoom_show=True)
        bar.render('order.html')


    # 产品“ChickenBowl”, 选择最多的配料
    def mater_chichenBowl_is_most(self):
        """
        首先要找出所有的ChickenBowl 的订单，然后在计算chicken_bowl中
        出现最多的配料
        :return:
        """
        #找出所有的ChickenBowl 的订单
        info=self.df[(self.df['item_name']=='Chicken Bowl')]
        # 选择出所有chickenbowl的配料表
        matertil=info['choice_description']
        matertil_liang=matertil.sort_values()
        detial_info=matertil_liang.describe()
        most_material=detial_info['top']

        # print(uu)
        # print(matertil_liang)
        return most_material

   # 产品“ChickenBowl”每种配料选择的次数，及所占比例，并做圆饼图
    def number_of_times_and_proportion_of_each_ingredient_selected(self):
        info = self.df[(self.df['item_name'] == 'Chicken Bowl')]
        # 选择出所有chickenbowl的配料表
        matertil = info['choice_description']
        # 计算每种相同的订单出现的次数
        name=matertil.sort_values().unique()
        print(len(name))
        print(name)
        matertil_liang = matertil.sort_values()
        # print(len(matertil_liang))
        per_order_liang=matertil_liang.value_counts()
        # print(per_order_liang)
        # print(len(per_order_liang))
        # hua=self.df.value_counts(ap,sort=True)
        # print(hua)
        #  一共有配料的个数
        # print(matertil)
        num=len(per_order_liang)
        # print(num)
        rate=per_order_liang/num
        # print(rate)
        pie=Pie('配料饼图')
        pie.add('每种配料选择的次数，及所占比例',name,rate,is_label_show=True,is_more_utils=True)
        pie.render('material.html')
        # 找出所有配料出现的次数，也就是同一订单出现的次数

        # print(len( matertil.sort_values().unique()))
        # print(matertil_liang)
        # print(matertil_liang.sum())





if __name__=="__main__":
    x=ChipotleNan()

    info=x.product_kinds_and_brand_material_price_to_exls()
    print(info)



























